class Solution:
    def maxChunksToSorted(self, arr):
        st = [[arr[0], arr[0]]]
        for num in arr[1:]:
            max_val = st[-1][1]
            while st and st[-1][0] > num:
                st.pop()
            if st:
                if st[-1][1] > num:
                    st[-1] = [st[-1][0], max_val]
                else:
                    st.append([num, max(max_val, num)])
            else:
                st.append([num, max_val])
        return len(st)


data = Solution()
arr = [5, 4, 3, 2, 1]
print(data.maxChunksToSorted(arr))
arr = [2, 1, 3, 4, 4]
print(data.maxChunksToSorted(arr))
